//
//   RechargeController.swift
//  SCA
//
//  Created by tjun on 2025/9/7.
//

import UIKit
import SnapKit

class RechargeNetworkAddressView: UIView {
    
    let networkNameLabel = UILabel(font: UIFontSemiboldMake(24), textColor: .white)
    let addressValueLabel = UILabel(font: UIFontMediumMake(15), textColor: .white)
    let copyButton = UIButton(image: UIImage(named: "share_copy"))

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let networkView = UIView().addTo(self)
        networkView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(80)
        }
        
        let networkLabel = UILabel(text: "网络".localizedString, font: UIFontMake(12), textColor: UIColorMake(0x919191))
        networkLabel.addTo(networkView)
        networkLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(16)
        }
        
        networkNameLabel.addTo(networkView)
        networkNameLabel.snp.makeConstraints { make in
            make.left.equalTo(networkLabel)
            make.top.equalTo(networkLabel.snp.bottom).offset(12)
        }
        
        let lineView = UIView().addTo(networkView)
        lineView.backgroundColor = UIColor.white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(networkView.snp.bottom).offset(8)
        }
        
        let addressView = UIView().addTo(self)
        addressView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        let addressLabel = UILabel(text: "充值地址".localizedString, font: UIFontMake(12), textColor: UIColorMake(0x919191))
        addressLabel.addTo(addressView)
        addressLabel.snp.makeConstraints { make in
            make.left.equalTo(networkLabel)
            make.top.equalTo(AppStyle.margin.left)
        }

        addressValueLabel.numberOfLines = 0
        addressValueLabel.addTo(addressView)
        addressValueLabel.snp.makeConstraints { make in
            make.left.equalTo(addressLabel)
            make.right.equalTo(-46)
            make.top.equalTo(addressLabel.snp.bottom).offset(12)
            make.bottom.equalTo(-AppStyle.margin.left)
        }
        
        copyButton.addTo(addressView)
        copyButton.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalTo(addressValueLabel)
            make.height.width.equalTo(30)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class RechargeNetworkController: BaseController {
        
    private var datas: [RechargeModel] = []
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 150
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(RechargeNetworkCell.self, forCellReuseIdentifier: RechargeNetworkCell.identifier)
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.title = "充值".localizedString
        
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navBarHeight + 10)
        }
        
        WalletService.loadRechargeData(coin: "usdt") { value, error in
            if let models = value {
                self.datas.removeAll()
                self.datas.append(contentsOf: models)
            }
            self.tableView.reloadData()
        }
        
    }

}

extension RechargeNetworkController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: RechargeNetworkCell.identifier, for: indexPath) as! RechargeNetworkCell
        let model = datas[indexPath.row]
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let model = datas[indexPath.row]
        let detailController = RechargeController(model: model)
        present(detailController, animated: true)
    }
    
}

 
